<div class="assignments view">
<h2><?php  __('Assignment');?>: <?php echo $assignment['Assignment']['title']; ?></h2>
	<h3><?php  __('Instruction');?></h3>
	<p><?php echo $assignment['Assignment']['description']; ?></p><br />
	
	<h3><?php  __('Details');?></h3>
	<dl><?php $i = 0; $class = ' class="altrow"';?>
		<dt<?php if ($i % 2 == 0) echo $class;?>><?php __('Id'); ?></dt>
		<dd<?php if ($i++ % 2 == 0) echo $class;?>>
			<?php echo $assignment['Assignment']['id']; ?>
			&nbsp;
		</dd>
		<dt<?php if ($i % 2 == 0) echo $class;?>><?php __('Project'); ?></dt>
		<dd<?php if ($i++ % 2 == 0) echo $class;?>>
			<?php echo $assignment['Project']['title']; ?>
			&nbsp;
		</dd>
		

		<dt<?php if ($i % 2 == 0) echo $class;?>><?php __('Created'); ?></dt>
		<dd<?php if ($i++ % 2 == 0) echo $class;?>>
			<?php echo $assignment['Assignment']['created']; ?>
			&nbsp;
		</dd>
		<dt<?php if ($i % 2 == 0) echo $class;?>><?php __('Modified'); ?></dt>
		<dd<?php if ($i++ % 2 == 0) echo $class;?>>
			<?php echo $assignment['Assignment']['modified']; ?>
			&nbsp;
		</dd>
		<dt<?php if ($i % 2 == 0) echo $class;?>><?php __('Status'); ?></dt>
		<dd<?php if ($i++ % 2 == 0) echo $class;?>>
			<?php echo __($assignment['Assignment']['status']); ?>
			&nbsp;
			
			
			
		</dd>
		
		
		
		
	</dl>
	
	
	
	<script type="text/javascript"> 
		function checkForm()
		{
			/* var x=document.forms["ProjectViewForm"]["code"].value.trim();
			if (x==null || x=="") {
				alert("Code must be filled out");
				return false;
			}
			*/
			document.ProjectViewForm.submit()
		}	
		
		function checkFormConfirm()
		{
			/* var x=document.forms["ProjectViewForm"]["code"].value.trim();
			if (x==null || x=="") {
				alert("Code must be filled out");
				return false;
			}
			*/
			var confirm1 = confirm('WARNING: Locking up an assignment and not doing it will result in your banning from this project. \r\n\r\n Are you surely want to lock this?');
			
			if (confirm1) {			
				document.ProjectViewForm.submit();
			}
		}			
	</script> 
	
	
	<?php 
			if ($assignment['Assignment']['status'] == 'available') {
?>
			<br />
			<div>
			<form action="<?php echo $this->base; ?>/assignments/view/<?php echo $assignment['Assignment']['id']; ?>" id="ProjectViewForm" method="post" name="ProjectViewForm" accept-charset="utf-8">
			<input type="hidden" name="_method" value="POST" />
			
			<input type="hidden" name="status" value="locked" />
			<div class="submit"><h3>Do you want to do this assignment?</h3><input onclick="checkFormConfirm()" type="button" value="Click here to Reserve/Lock it" /></div>
			</form>			
			</div>
			
<?php			
			} elseif($this->Session->read('User.id') == $assignment['Assignment']['user_id']) {
				if (($assignment['Assignment']['status'] == 'locked' || $assignment['Assignment']['status'] == 'revision')) {
				
				
				/************** countdown timer starts *******************/
				
				$secs = ($assignment['Assignment']['status'] == 'locked')?Configure::read('TimeElapseBan'):Configure::read('TimeElapseFlag');
				
				$db   = ConnectionManager::getInstance(); 
				$conn = $db->getDataSource('default'); 
				$sql = "SELECT ADDDATE(datetime_started, INTERVAL $secs SECOND) secs FROM `assignments` WHERE id='".$assignment['Assignment']['id']."'";
				$res  = $conn->query($sql);
				//var_dump($res);exit;
				
				$countdown_to = $res[0][0]['secs']; // 24-Hour Format: YYYY-MM-DD HH:MM:SS"
				
				$sql = "SELECT now() secs";
				$res  = $conn->query($sql);
				
				$count_from   = $res[0][0]['secs']; // current time -- NO NEED TO CHANGE
				
				function datediff($interval, $datefrom, $dateto, $using_timestamps = false) {
				  /*
					$interval can be:
					yyyy - Number of full years
					q - Number of full quarters
					m - Number of full months
					y - Difference between day numbers
					  (eg 1st Jan 2004 is "1", the first day. 2nd Feb 2003 is "33". The datediff is "-32".)
					d - Number of full days
					w - Number of full weekdays
					ww - Number of full weeks
					h - Number of full hours
					n - Number of full minutes
					s - Number of full seconds (default)
				  */
				  
				  if (!$using_timestamps) {
					$datefrom = strtotime($datefrom, 0);
					$dateto = strtotime($dateto, 0);
				  }
				  $difference = $dateto - $datefrom; // Difference in seconds
				   
				  switch($interval) {
				   
					case 'yyyy': // Number of full years

					  $years_difference = floor($difference / 31536000);
					  if (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom), date("j", $datefrom), date("Y", $datefrom)+$years_difference) > $dateto) {
						$years_difference--;
					  }
					  if (mktime(date("H", $dateto), date("i", $dateto), date("s", $dateto), date("n", $dateto), date("j", $dateto), date("Y", $dateto)-($years_difference+1)) > $datefrom) {
						$years_difference++;
					  }
					  $datediff = $years_difference;
					  break;

					case "q": // Number of full quarters

					  $quarters_difference = floor($difference / 8035200);
					  while (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom)+($quarters_difference*3), date("j", $dateto), date("Y", $datefrom)) < $dateto) {
						$months_difference++;
					  }
					  $quarters_difference--;
					  $datediff = $quarters_difference;
					  break;

					case "m": // Number of full months

					  $months_difference = floor($difference / 2678400);
					  while (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom)+($months_difference), date("j", $dateto), date("Y", $datefrom)) < $dateto) {
						$months_difference++;
					  }
					  $months_difference--;
					  $datediff = $months_difference;
					  break;

					case 'y': // Difference between day numbers

					  $datediff = date("z", $dateto) - date("z", $datefrom);
					  break;

					case "d": // Number of full days

					  $datediff = floor($difference / 86400);
					  break;

					case "w": // Number of full weekdays

					  $days_difference = floor($difference / 86400);
					  $weeks_difference = floor($days_difference / 7); // Complete weeks
					  $first_day = date("w", $datefrom);
					  $days_remainder = floor($days_difference % 7);
					  $odd_days = $first_day + $days_remainder; // Do we have a Saturday or Sunday in the remainder?
					  if ($odd_days > 7) { // Sunday
						$days_remainder--;
					  }
					  if ($odd_days > 6) { // Saturday
						$days_remainder--;
					  }
					  $datediff = ($weeks_difference * 5) + $days_remainder;
					  break;

					case "ww": // Number of full weeks

					  $datediff = floor($difference / 604800);
					  break;

					case "h": // Number of full hours

					  $datediff = floor($difference / 3600);
					  break;

					case "n": // Number of full minutes

					  $datediff = floor($difference / 60);
					  break;

					default: // Number of full seconds (default)

					  $datediff = $difference;
					  break;
				  }    

				  return $datediff;
				}

				// getting Date difference in SECONDS
				$diff = datediff("s", $count_from, $countdown_to);
?>
				
				<br />
				
				<h3>Please commit the code before <b id='cd'></b></h3>
				<script type="text/javascript"> 
				// Here’s where the Javascript starts
				countdown = <?=$diff?>;

				// Converting date difference from seconds to actual time
				function convert_to_time(secs)
				{
					secs = parseInt(secs);	
					hh = secs / 3600;	
					hh = parseInt(hh);	
					mmt = secs - (hh * 3600);	
					mm = mmt / 60;	
					mm = parseInt(mm);	
					ss = mmt - (mm * 60);	
						
					if (hh > 23)	
					{	
					   dd = hh / 24;	
					   dd = parseInt(dd);	
					   hh = hh - (dd * 24);	
					} else { dd = 0; }	
						
					if (ss < 10) { ss = "0"+ss; }	
					if (mm < 10) { mm = "0"+mm; }	
					if (hh < 10) { hh = "0"+hh; }	
					if (dd == 0) { return (hh+":"+mm+":"+ss); }	
					else {	
						if (dd > 1) { return (dd+" days "+hh+":"+mm+":"+ss); }
						else { return (dd+" day "+hh+":"+mm+":"+ss); }
					}	
				}

				// Our function that will do the actual countdown
				function do_cd()
				{
					if (countdown < 0)	
					{ 	
						document.getElementById('cd').innerHTML = "00:00:00 Secs. Sorry!! Time Elapsed!!!";
					}	
					else	
					{	
						document.getElementById('cd').innerHTML = convert_to_time(countdown);
						setTimeout('do_cd()', 1000);
					}	
					countdown = countdown - 1;	
				}
				do_cd();
				</script>
				
<?php				
				/******************** countdown timer ends **********************/
?>
			<br />
			<?php 
			if ($assignment['Assignment']['status'] == 'revision') {
			?>
			<h3>Note: Revision needed for the submitted code!!!</h3>
			<?php
			}
			?>
			<div>
				<form action="<?php echo $this->base; ?>/assignments/view/<?php echo $assignment['Assignment']['id']; ?>" id="ProjectViewForm" method="post" name="ProjectViewForm" accept-charset="utf-8">
				<fieldset>	
					<legend>
					Please input your completed work Here
					</legend>
					<div>
						<textarea rows="10" cols="20" name="code"><?php echo isset($this->params['form']['code'])?$this->params['form']['code']:''; ?></textarea>
					</div>
					<div class="submit"><input type="button" onclick="checkForm()" value="Click here to Submit!" /></div>

				</fieldset>	
				</form>		
			</div>
<?php					
				} elseif ($assignment['Assignment']['status'] == 'under_review') {
?>
				<br /><h4><b><?php echo __('You have submitted your code. The code is now under review'); ?></b></h4>
<?php				
				}
			}			
			?>
			
</div>
